/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-1-18 下午7:44 Created by dwzhang
 */
package com.jade.modules.template.impl;

import java.util.Date;
import java.util.List;
import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Order;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.modules.template.Template;
import com.jade.modules.template.TemplateDao;
import com.jade.modules.template.TemplateException;
import com.jade.modules.template.TemplateManager;
import org.springframework.transaction.annotation.Transactional;

/**
 * 模版管理器实现。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-1-18 下午7:44
 */
//@Named ("module_templateManager")
public class TemplateManagerImpl implements TemplateManager
{
    protected TemplateDao templateDao;

//    @Inject
    public void setTemplateDao(@Named ("module_templateDao") TemplateDao templateDao)
    {
        this.templateDao = templateDao;
    }

    protected Template getByName(String ownerType, String ownerId, String type, String name)
    {
        Criteria criteria = new Criteria(0, 1);
        criteria.add(Restrictions.eq("ownerType", ownerType));
        criteria.add(Restrictions.eq("ownerId", ownerId));
        criteria.add(Restrictions.eq("type", type));
        criteria.add(Restrictions.eq("name", name));
        List<Template> templates = list(criteria);
        return templates.isEmpty() ? null : templates.get(0);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void add(Template template)
            throws TemplateException
    {
        if (getByName(template.getOwnerType(), template.getOwnerId(), template.getType(), template.getName()) != null) {
            throw new TemplateException("err.module.template.name_conflict", "name conflict", null, template.getName());
        }
        template.setCreationDate(new Date());
        template.setUpdateDate(template.getCreationDate());
        templateDao.add(template);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void update(Template template)
            throws TemplateException
    {
        Template old = getByName(template.getOwnerType(), template.getOwnerId(), template.getType(), template.getName());
        if (old != null && old.getId() != template.getId()) {
            throw new TemplateException("err.module.template.name_conflict", "name conflict", null, template.getName());
        }
        template.setUpdateDate(new Date());
        templateDao.update(template);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void remove(long... ids)
            throws TemplateException
    {
        for (long id : ids) {
            Template template = get(id);
            if (template != null) {
                templateDao.remove(template);
            }
        }
    }

    public Template get(long id)
    {
        return templateDao.get(id);
    }

    public List<Template> list(Criteria criteria)
    {
        return templateDao.list(criteria);
    }

    public PagingList<Template> query(Criteria criteria)
    {
        return templateDao.query(criteria);
    }

    public Template getSingle(String type, String businessType)
    {
        Criteria criteria = new Criteria(0, 1);
        criteria.add(Restrictions.eq("status", Template.STATUS_NORMAL));
        criteria.add(Restrictions.eq("type", type));
        criteria.add(Restrictions.eq("businessType", businessType));
        criteria.addOrder(Order.desc("sortCode"));
        List<Template> templates = templateDao.list(criteria);
        return templates.isEmpty() ? null : templates.get(0);
    }
}
